package com.ldd.mybatis.controller;

import com.ldd.mybatis.model.MyApiResponse;
import com.ldd.mybatis.model.Dept;
import com.ldd.mybatis.model.DeptAndEmp;
import com.ldd.mybatis.model.EmpAndDept;
import com.ldd.mybatis.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
public class DeptController {
    @Autowired
    private DeptService deptService;

    //http://localhost:8080/getDeptById?id=1
    @GetMapping(value = "/getDeptById")
    public MyApiResponse<Dept> getDeptById(@RequestParam("id") int id) {
        log.info("getDeptById id:{}", id);
        Dept dept = deptService.selectDeptById(id);
        return MyApiResponse.success(dept);
    }

    //http://localhost:8080/selectDeptAndEmpById?name=SALES
    @GetMapping(value = "/selectDeptAndEmpById")
    public MyApiResponse<List<DeptAndEmp>> selectDeptAndEmpById(@RequestParam("name") String name) {
        log.info("selectDeptAndEmpById name:{}", name);
        List<DeptAndEmp> list = deptService.selectDeptAndEmpById(name);
        return MyApiResponse.success(list);
    }
    //http://localhost:8080/selectEmpAndDept
    @GetMapping(value = "/selectEmpAndDept")
    public MyApiResponse<List<EmpAndDept>> selectEmpAndDept() {
        log.info("selectEmpAndDept");
        List<EmpAndDept> list = deptService.selectEmpAndDept();
        return MyApiResponse.success(list);
    }
}
